var KOC = require("koc.common.utils");
var Config = require("../config");
var StateModel = require("./statemodel");
var DALFactory = require("../lib/dal/dal-factory");

var StateData = module.exports = {
    Value: function (req, res) {
        if (!res.locals.Data) {
            res.locals.Data = req.session.Data || StateModel.New();
        }
        return res.locals.Data;
    },
    Set: function (req, res, data) {
        res.locals.Data = data;
        req.session.Data = res.locals.Data;
    },
    Clear: function (req, res) {
        res.locals.Data = StateModel.New();
        req.session.Data = res.locals.Data;
    },
    Cookie: {
        UVID: {
            Get: function (req, res) {
                var _UVID = null;
                if (req && req.cookies) {
                    _UVID = KOC.StringUtils.ToString(req.cookies["UVID"]);
                }
                if (!_UVID) {
                    _UVID = KOC.Uuid.v4();
                    res.cookie("UVID", _UVID, {
                        domain: Config.Common.Domain
                    });
                }
                return _UVID;
            }
        },
        ChannelID: {
            Set: function (res, data) {
                if (!data) {
                    return;
                }
                res.cookie("ChannelID", KOC.StringUtils.ToString(data), {
                    domain: Config.Common.Domain
                });
            },
            Get: function (req) {
                return req && req.cookies ? KOC.StringUtils.ToString(req.cookies["ChannelID"]) : null;
            }
        },
        InviterID: {
            Set: function (res, data) {
                if (!data) {
                    return;
                }
                res.cookie("InviterID", KOC.StringUtils.ToIntPositive(data), {
                    domain: Config.Common.Domain
                });
            },
            Get: function (req) {
                return req && req.cookies ? KOC.StringUtils.ToInt(req.cookies["InviterID"]) : null;
            }
        },
        RoomID: {
            Set: function (res, data) {
                res.cookie("RoomID", KOC.StringUtils.ToIntPositive(data, -1), {
                    domain: Config.Common.Domain
                });
            },
            Get: function (req) {
                return req && req.cookies ? KOC.StringUtils.ToInt(req.cookies["RoomID"]) : null;
            }
        },
        SignUp_Url: {
            Set: function (res, data) {
                res.cookie("SignUp_Url", KOC.StringUtils.ToString(data), {
                    domain: Config.Common.Domain
                });
            },
            Get: function (req) {
                return req && req.cookies ? KOC.StringUtils.ToString(req.cookies["SignUp_Url"]) : null;
            }
        },
        Remember: {
            Set: function (res, LoginName, LoginPassword) {
                res.cookie('LINFO', {
                    LN: LoginName,
                    LP: LoginPassword
                }, {
                    domain: Config.Common.Domain,
                    maxAge: 30 * 24 * 60 * 60 * 1000
                });
            },
            Clear: function (res) {
                res.clearCookie('LINFO', {domain: Config.Common.Domain});
            },
            Get: function (req) {
                return req && req.cookies ? req.cookies["LINFO"] : null;
            },
            Login: function (req, res, callback) {
                if (StateData.Value(req, res).IsLogin) {
                    callback(true);
                    return;
                }
                var _LoginInfo = StateData.Cookie.Remember.Get(req);
                if (!_LoginInfo) {
                    callback();
                    return;
                }
                DALFactory.Business.User_Login({
                    LoginName: _LoginInfo.LN,
                    Password: _LoginInfo.LP,
                    SignIn_Origin: 0,
                    SignIn_Type: 1,
                    Create_IP: KOC.StringUtils.GetClientIP(req)
                }, function (retValue) {
                    if (retValue.hasError) {
                        StateData.Cookie.Remember.Clear(res);
                        callback();
                        return;
                    }
                    //写入session
                    StateData.Set(req, res, retValue.returnObject);
                    callback(true);
                });
            }
        }
    },
    RightCode: {
        Admin: function (req, res, code) {
            var _Data = StateData.Value(req, res);
            if (!_Data.IsLogin) {
                return -1;//未登录
            }
            if (!_Data.IsAdmin) {
                return -2;//身份错误
            }
            if (_Data.Admin.IsMaster || !code) {
                return 0;//正常
            }
            var _RightCodeStatus = -3;//没有权限
            KOC.Lodash.forEach(KOC.Lodash.compact(code.split("|")), function (ThisValue) {
                if (_Data.Admin.RightCode.indexOf(ThisValue) >= 0) {
                    _RightCodeStatus = 0;
                    return false;
                }
            });
            return _RightCodeStatus;
        },
        Family: function (req, res) {
            var _Data = StateData.Value(req, res);
            if (!_Data.IsLogin) {
                return -1;//未登录
            }
            if (!_Data.IsFamily) {
                return -2;//身份错误
            }
            if (_Data.Family.IsMaster || !code) {
                return 0;//正常
            }
            var _RightCodeStatus = -3;//没有权限
            KOC.Lodash.forEach(KOC.Lodash.compact(code.split("|")), function (ThisValue) {
                if (_Data.Family.RightCode.indexOf(ThisValue) >= 0) {
                    _RightCodeStatus = 0;
                    return false;
                }
            });
            return _RightCodeStatus;
        }
    }
};